<template>
  <el-dialog :title="!dataForm.userId ? '新增' : '修改'"
             :close-on-click-modal="false"
             :visible.sync="visible">
    <el-form :model="dataForm"
             ref="dataForm"
             @keyup.enter.native="dataFormSubmit()"
             label-width="80px">
      <el-form-item label="延长开关" prop="enableDelay">
        <el-switch v-model="dataForm.enableDelay"></el-switch>
      </el-form-item>
      <el-form-item label="延迟时间" prop="delayTime">
        <el-button @click="decrease('delayTime')" class="decrease">-</el-button>
        <el-input v-model="dataForm.delayTime" class="delayTime" style="width: 150px;">
          <template #append><span>秒</span></template>
        </el-input>
        <el-button @click="increase('delayTime')" class="increase">+</el-button>
      </el-form-item>
    </el-form>
    <span slot="footer"
          class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary"
                 @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data () {
    return {
      visible: false,
      dataForm: {
        id: 0,
        enableDelay: '',
        delayTime: ''
      }
    }
  },
  methods: {
    init (id) {
      this.dataForm.id = id || 0
      this.visible = true
      this.$nextTick(() => {
        this.$refs.dataForm.resetFields()
      })
      if (this.dataForm.id) {
        this.$http({
          url: this.$http.adornUrl(`/v1/merchant/member/get/delay/time`),
          method: 'get',
          params: this.$http.adornParams({
            id: this.dataForm.id
          })
        }).then(({ data }) => {
          console.log(data.data)
          this.dataForm.id = data.data.memberId
          this.dataForm.enableDelay = data.data.enableDelay==0?false:true
          this.dataForm.delayTime = data.data.delayTime
        })
      }
    },
    increase(field) {
      this.dataForm[field] = (this.dataForm[field] || 0) + 1;
    },
    decrease(field) {
      this.dataForm[field] = (this.dataForm[field] || 0) - 1;
    },
    // 表单提交
    dataFormSubmit () {
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/v1/merchant/member/enable/delay/time`),
            method: 'post',
            data: this.$http.adornData({
              id: this.dataForm.id,
              enableDelay: this.dataForm.enableDelay?1:0,
              delayTime: this.dataForm.delayTime
            })
          }).then(({ data }) => {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.visible = false
                this.$emit('refreshDataList', this.page)
              }
            })
          })
        }
      })
    }
  }
}
</script>
